package com.netty.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

import com.netty.coder.Server2ServerDecoder;
import com.netty.coder.Server2ServerEncoder;
import com.netty.handler.ServerChannelHandler;

/**
 * 
 *
 * @author zhoufang
 *
 * 2018年1月9日 下午2:55:21
 */
public class ServerChannelInitializer extends ChannelInitializer<NioSocketChannel>{

	@Override
	protected void initChannel(NioSocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		pipeline.addLast(new IdleStateHandler(12, 0, 0,TimeUnit.SECONDS));
		pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
		pipeline.addLast("ssEncoder", new Server2ServerEncoder());
		pipeline.addLast("ssDecoder", new Server2ServerDecoder());
		pipeline.addLast("serverHandler",new ServerChannelHandler());
	}

}
